<?php

$header_link = sprintf('/%s/%s/%s', $this->params['module'], $this->params['controller'], $this->params['action']);
$extra_link = sprintf('/%s/%s', $this->params['module'], $this->params['controller']);
$direction = array(
   'id' => ($this->params['sort'] == 'sort:id' && $this->params['direction'] == 'direction:asc' ? 'desc' : 'asc'),
   'firstname' => ($this->params['sort'] == 'sort:firstname' && $this->params['direction'] == 'direction:asc' ? 'desc' : 'asc'),
   'lastname' => ($this->params['sort'] == 'sort:lastname' && $this->params['direction'] == 'direction:asc' ? 'desc' : 'asc'),
   'company' => ($this->params['sort'] == 'sort:company' && $this->params['direction'] == 'direction:asc' ? 'desc' : 'asc'),
   'city' => ($this->params['sort'] == 'sort:city' && $this->params['direction'] == 'direction:asc' ? 'desc' : 'asc'),
   'country' => ($this->params['sort'] == 'sort:country' && $this->params['direction'] == 'direction:asc' ? 'desc' : 'asc')
);
?>

<div class="<?php echo $this->params['controller']; ?> index">
<table class="list_table">
	<tr class="header">
		<th><a href="<?php echo $header_link; ?>/page:1/sort:id/direction:<?php echo $direction['id']; ?>">Id</a></th>
		<th><a href="<?php echo $header_link; ?>/page:1/sort:firstname/direction:<?php echo $direction['firstname']; ?>">Firstname</a></th>
		<th><a href="<?php echo $header_link; ?>/page:1/sort:lastname/direction:<?php echo $direction['lastname']; ?>">Lastname</a></th>
		<th><a href="<?php echo $header_link; ?>/page:1/sort:company/direction:<?php echo $direction['company']; ?>">Company</a></th>
		<th><a href="<?php echo $header_link; ?>/page:1/sort:city/direction:<?php echo $direction['city']; ?>">City</a></th>
		<th><a href="<?php echo $header_link; ?>/page:1/sort:country/direction:<?php echo $direction['country']; ?>">Country</a></th>
		<th class="action_header">Actions</th>
	</tr>
	<?php
      if(count($this->paginator)) {
         $counter = 0;
      	foreach ($this->paginator as $entry) {
            echo '<tr class="' . ($counter++ % 2 == 0 ? 'row2' : 'row1') . '">'.PHP_EOL;
            echo sprintf('<td class="id">%s</td>', $this->escape($entry['id']));
            echo sprintf('<td>%s</td>', $this->escape($entry['firstname']));
            echo sprintf('<td>%s</td>', $this->escape($entry['lastname']));
            echo sprintf('<td>%s</td>', $this->escape(str_replace('<br />', ' ', $entry['company'])));
            echo sprintf('<td>%s</td>', $this->escape($entry['city']));
            echo sprintf('<td>%s</td>', $this->escape($entry['country']));
            
            $actions = array(
               'edit' => array(
                  'class' => 'edit',
                  'link_text' => '[E]',
                  'link' => '/admin/' . $this->params['controller'] . '/edit/id/' . $this->escape($entry['id'])
               ),
               'delete' => array(
                  'class' => 'delete',
                  'link_text' => '[D]',
                  'link' => '/admin/'.$this->params['controller'].'/delete/id/' . $this->escape($entry['id'])
               )
            );
            $act = '';
            foreach($actions as $action) {
               $link = sprintf('<a class="%s" href="%s">%s</a> ', $action['class'], $action['link'], $action['link_text']);
               $act .= $link;
            }
            echo sprintf('<td class="action_cell">%s</td>', $act);
            echo '</tr>'.PHP_EOL;
      	}
      	
      }
   ?>
</table>
<div class="extra_actions">
   <ul>
      <li><a href="<?php echo $extra_link; ?>/add">Add new</a></li>
      <li><a href="<?php echo $extra_link; ?>/import">Import from folder</a></li>
   </ul>
</div>
<?php echo $this->paginator; ?>
</div>